package com.itheima.health.job;

import com.itheima.health.common.RedisConst;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Set;

@Slf4j
public class CleanImgageJob extends QuartzJobBean {

    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetMealService setMealService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("请理垃圾图片");
        Set<String> members = redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).members();

        for (String member : members) {
            Integer count = setMealService.countByImg(member);
            if (count==0) {
                qiniuUtils.deleteFileFromQiniu(member);
            }
        redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(member);
        }
        log.info("请理垃圾图片结束");
    }
}
